package tk.leafotto.sign2.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import tk.leafotto.sign2.R;
import tk.leafotto.sign2.utils.FileUtil;

public class RestoreFilesActivity extends Activity {

    private ArrayList<String> restoreFileList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_restore_files);
        TextView tv_nofile = (TextView) findViewById(R.id.tv_nofile);
        ListView lv_file = (ListView) findViewById(R.id.lv_file);
        FileUtil fileUtil = new FileUtil();
        restoreFileList = fileUtil.getRestoreFileList();
        if (restoreFileList == null || restoreFileList.isEmpty()) {
            tv_nofile.setVisibility(View.VISIBLE);
            lv_file.setVisibility(View.GONE);
        }else{
            tv_nofile.setVisibility(View.GONE);
            lv_file.setVisibility(View.VISIBLE);
            FileAdapter fileAdapter = new FileAdapter();
            lv_file.setAdapter(fileAdapter);
        }
    }

    class FileAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return restoreFileList.size();
        }

        @Override
        public Object getItem(int position) {
            return restoreFileList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = convertView.inflate(getApplicationContext(),R.layout.item_restore,null);
            }

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("fileName",restoreFileList.get(position));
                    setResult(10,intent);
                    finish();
                }
            });

            TextView tv_restore_file = (TextView) convertView.findViewById(R.id.tv_restore_file);
            tv_restore_file.setText(restoreFileList.get(position));

            return convertView;
        }
    }
}
